package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC388 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
         * @param 此题目参考了别人代码
         * 这是因为问题情况较为复杂
         * 而自己又没做出来，又要吃饭，没办法
         * 可以借鉴思想，多多学习
         * @return:
         */
    class Solution {
        public int lengthLongestPath(String input) {
            if (null == input) return 0;

            int curMaxFileLength = 0;

            String[] dirList = input.split("\n");
            int[] depthLengthSum = new int[input.length() + 1];

            for (String dir : dirList) {
                int curDepth = dir.lastIndexOf("\t") + 2;
                int curLen = dir.length() - (curDepth - 1);

                if (dir.contains(".")) {
                    curMaxFileLength = Math.max(curMaxFileLength, depthLengthSum[curDepth - 1] + curLen);
                } else {
                    //当前是目录，+1是因为目录有反斜杠/
                    depthLengthSum[curDepth] = depthLengthSum[curDepth - 1] + curLen + 1;
                }
            }

            return curMaxFileLength;
        }

    }

    public void run() {
        Solution solution = new Solution();
        String dir = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";
        System.out.println(solution.lengthLongestPath(dir));
    }

    public static void main(String[] args) throws Exception {
        LC388 an = new LC388();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
